home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3047 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.9 KB  |  96 lines

  1. Path: unidhp.uni-c.dk!phower
  2. From: phower@unidhp.uni-c.dk (Peter Henriksen)
  3. Newsgroups: comp.lang.c++
  4. Subject: Core-dumps in g++ 2.7.0 when redefining new/delete
  5. Date: 21 Jan 1996 22:19:59 GMT
  6. Organization: News Server at UNI-C, Danish Computing Centre for Research and Education.
  7. Message-ID: <4due6f$5f1@news.uni-c.dk>
  8. NNTP-Posting-Host: unidhp.uni-c.dk
  9. Summary: Core-dumps in g++ 2.7.0 when redefining new/delete
  10. Keywords: new/delete
  11. X-Newsreader: TIN [version 1.2 PL1]
  12.  
  13. Core-dumps in g++ 2.7.0 when redefining new/delete.
  14.  
  15. We are having problems with GNU g++ 2.7.0 when redefining the new/delete operators.
  16. A code example is shown below. It core-dumps during the free call.
  17.  
  18. We believe that the problem originates from the fact that the new operator is not
  19. returning the malloc address. To get this specific example to core-dump, we need
  20. filling the allocated space, but other (larger) examples will core-dump just by 
  21. shifting the pointer returned by malloc.
  22.  
  23. The compiler was taken from Slackware 3.0.0, and the op. sys. we are running is Linux 
  24. 1.2.13. Other compilers (GNU g++ 2.6.3, SGI CC) work without problems.
  25.  
  26. Any ideas why this is happening? Are we doing something wrong, or is there a bug in
  27. the compiler? Is there a change in the C++ standard that we are not aware of?
  28.  
  29.       Thanks in advance
  30.       Peter
  31.  
  32. PS : Please mail me directly (phower at unidhp.uni-c.dk)
  33.  
  34.  
  35. ////////////////////////////////////CODE EXAMPLE///////////////////////////////////
  36. #include <iostream.h>
  37. #include <strstream.h>
  38. #include <malloc.h>
  39.  
  40. // Shift the malloc address 16 bytes. This way we should be certain that we do not
  41. // violate any alignment restrictions.
  42. #define EXTRA 16
  43.  
  44. void * operator new ( size_t user_size ) {
  45.   // allocate requested size + EXTRA
  46.   size_t size = user_size + EXTRA;
  47.   void* pointer = malloc(size);
  48.  
  49.   // fill complete space with 0xff
  50.   char* destroy = (char*) pointer;
  51.   for ( long i = 0; i < size; i++, destroy++ ) *destroy = 0xff;
  52.   
  53.   // return malloc pointer + EXTRA
  54.   char* user_pointer = ((char*) pointer) + EXTRA;
  55.   return user_pointer;
  56. }
  57. void operator delete ( void* dead_obj ) {
  58.   // correct pointer so we free the same address we got from malloc
  59.   char* user_pointer = ((char*) dead_obj) - EXTRA;
  60.   free((void*) user_pointer);
  61. }
  62.  
  63. // OStr just used to give us the core-dump. It has probably nothing to
  64. // do with the problem. In this example, a char[] is allocated and a
  65. // ostrstream is attached.
  66. class OStr { 
  67. public:
  68.   OStr(int size) {
  69.     string_ = new char[size];                   
  70.     string_stream_ = new ostrstream(string_,size); 
  71.   }
  72.   ~OStr() {
  73.     delete string_stream_; 
  74.     delete[] string_;
  75.   }
  76.  
  77.   char*       string_;
  78.   ostrstream* string_stream_;
  79. };
  80.  
  81.  
  82. void main(int argc, char** argv) {
  83.   static char str[] = "Something";
  84.   istrstream* str_stream = new istrstream(str);
  85.  
  86.   // next line only to get the core-dump.
  87.   OStr* new_string = new OStr(14);
  88.  
  89.   cout << "Before Delete" << flush;
  90.   delete str_stream;
  91.   cout << "... After Delete\n" << flush;
  92. }
  93.  
  94.  
  95.  
  96.